import 'package:flutter/material.dart';
import "package:flutter_bloc/flutter_bloc.dart";

import "../bloc/UserBloc.dart";
import "../config/GlobalThemData.dart";
import "../pojo/vo/UserVo.dart";
import "general/EventPageView.dart";
// 安卓平板
import "general/GeneralWelcome.dart";
import "general/Profile.dart";
import "pad/PadHome.dart";

class PadView extends StatefulWidget {
  final bool isDarkMode;

  const PadView({super.key, required this.isDarkMode});

  @override
  State<PadView> createState() => _PadViewState();
}

class _PadViewState extends State<PadView> {

  @override
  void initState() {
    super.initState();
    BlocProvider.of<UserBloc>(context).add(LoadUser());
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: widget.isDarkMode ? GlobalThemData.darkThemeData : GlobalThemData.lightThemeData,
      debugShowCheckedModeBanner: false,
      home: const _Tabs(),
    );
  }
}


class _Tabs extends StatefulWidget {
  const _Tabs({super.key});

  @override
  State<_Tabs> createState() => _TabsState();
}

class _TabsState extends State<_Tabs> {
  @override
  Widget build(BuildContext context) {
    return BlocBuilder<UserBloc, UserState>(
        builder: (context,state){
          if (state is UserLoading) {
            return const CircularProgressIndicator();
          } else if (state is UserLoaded) {
            final userViewModel = state.userViewModel;
            UserVo? currentUser = userViewModel.currentUser;
            // 若未登录
            if(currentUser == null){
              return const GeneralWelcome();
            }
            return const _TabView();
          } else {
            return Container();
          }
        },
    );
  }
}


class _TabView extends StatefulWidget {
  const _TabView({super.key});

  @override
  State<_TabView> createState() => _TabViewState();
}

class _TabViewState extends State<_TabView> with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  int _currentIndex = 0;  // 存储选中了哪一个页面
  final List<Widget> _pages = [
    const PadHome(),
    const EventPageView(),
    const Profile(),
  ];

  final List<String> _pageName = [
    "主页",
    "活动",
    "个人中心"
  ];

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Row(
        children: [
          NavigationRail(
            onDestinationSelected: (int index) {
              setState(() {
                _currentIndex = index;
              });
            },
            labelType: NavigationRailLabelType.all,
            groupAlignment: 0,
            minWidth: 110,
            selectedIconTheme: const IconThemeData(color: Colors.white),
            selectedLabelTextStyle: TextStyle(color: Theme.of(context).colorScheme.onPrimary),
            indicatorColor: Theme.of(context).colorScheme.primary,
            destinations: const <NavigationRailDestination>[
              NavigationRailDestination(
                padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
                icon: Icon(Icons.home),
                label: Text('主页'),
              ),
              NavigationRailDestination(
                padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
                icon: Icon(Icons.event),
                label: Text('活动'),
              ),
              NavigationRailDestination(
                padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
                icon: Icon(Icons.person),
                label: Text('个人中心'),
              ),
            ],
            selectedIndex: _currentIndex,
          ),
          const VerticalDivider(thickness: 1, width: 1),
          Expanded(
            child: _pages[_currentIndex],
          )
        ],
      )
    );
  }
}



